home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CUJ9206.ARJ / 1006032A < prev    next >
Text File  |  1992-06-02  |  1KB  |  51 lines

  1.  
  2.     #include <stdarg.h>         /* variable argument macros */ 
  3.     #include <stdio.h>          /* for printf */ 
  4.  
  5.     int maxn(int n, ...);       /* the function prototype for maxn */ 
  6.  
  7.     /* 
  8.     ** Main makes some calls to maxn, and prints the results. */ 
  9.     */ 
  10.     void main(void) 
  11.         { 
  12.         printf("MaxN tests:\n\n"); 
  13.         printf( 
  14.             "maxn(2, 3, 5) = %d\n", 
  15.              maxn(2, 3, 5) ); 
  16.         printf( 
  17.             "maxn(5, 3, 5, 7, 11, 13) = %d\n", 
  18.              maxn(5, 3, 5, 7, 11, 13) ); 
  19.         printf( 
  20.             "maxn(3, 3, 5, 7, 11, 13) = %d\n", 
  21.              maxn(3, 3, 5, 7, 11, 13) ); 
  22.         printf( 
  23.             "maxn(1, 9) = %d\n", 
  24.              maxn(1, 9) ); 
  25.         } 
  26.  
  27.     /* 
  28.     ** The maxn function returns the largest of n integer arguments. 
  29.     */ 
  30.     int maxn(int n, ...) 
  31.         { 
  32.         int val, max_val; 
  33.         va_list argp;       /* pointer to arguments */ 
  34.  
  35.         va_start(argp, n);  /* points argp to arg after n */ 
  36.  
  37.         max_val = va_arg(argp, int);    /* get first of n arguments */ 
  38.         while (--n) 
  39.             { 
  40.             val = va_arg(argp, int);    /* get subsequent arguments */ 
  41.             if (val > max_val) 
  42.                 max_val = val;          /* max_val gets the largest */ 
  43.             } 
  44.  
  45.         va_end(argp);       /* clean up */ 
  46.  
  47.         return max_val;     / return value of the largest */ 
  48.         } 
  49.  
  50.  
  51.